#ifndef T_LOCK_H
#define T_LOCK_H

#include <QtCore>
#include "t_global.h"

class T_RecursiveAtomic : public QMutex
{
public:
    explicit T_RecursiveAtomic() : QMutex(QMutex::Recursive)
    {  }
};

class T_RecursiveAtomicObject : public QObject, public T_RecursiveAtomic
{
    Q_OBJECT
public:
    explicit T_RecursiveAtomicObject() : T_RecursiveAtomic()
    {  }
};

class T_RecursiveLocked : public QReadWriteLock
{
public:
    explicit T_RecursiveLocked() : QReadWriteLock(QReadWriteLock::Recursive)
    {  }
};

class T_RecursiveLockedObject : public QObject, public T_RecursiveLocked
{
    Q_OBJECT
public:
    explicit T_RecursiveLockedObject() : T_RecursiveLocked()
    {  }
};

class T_SlimpleWaitCondition
{
public:
    explicit T_SlimpleWaitCondition()
        : m_wait_mutex(QMutex::NonRecursive)
    {  }
    bool wait(unsigned long a_msec = ULONG_MAX)
    {
        m_wait_mutex.lock();
        bool b = m_wait_cond.wait(&m_wait_mutex, a_msec);
        m_wait_mutex.unlock();
        return b;
    }
    void wakeOne()
    {
        m_wait_cond.wakeOne();
    }
    void wakeAll()
    {
        m_wait_cond.wakeAll();
    }
protected:
    QMutex m_wait_mutex;
    QWaitCondition m_wait_cond;
};

#define T_ATOMIC_LOCKER(V,X) QMutexLocker V(X);Q_UNUSED(V)
#define T_READ_LOCKER(V,X)   QReadLocker V(X);Q_UNUSED(V)
#define T_WRITE_LOCKER(V,X)  QWriteLocker V(X);Q_UNUSED(V)

#endif // T_LOCK_H
